package org.genew.squadra.xml.common;

import java.io.*;
import java.lang.reflect.Array;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.genew.squadra.xml.model.FileXHTML;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaveFileUtility {

	
	
	public static void createFileForUser(String username, String homePathRepository){
		
		String path = homePathRepository + username;
		System.out.println("Path della directory " + path);
		File cartella = new File(path);
		if(!cartella.exists()){
			cartella.mkdirs();
		}
		
		String pathUp = path+ File.separator + "uploadedFile";
		File cartellaUploaded = new File(pathUp);
		if(!cartellaUploaded.exists()){
			cartellaUploaded.mkdirs();
		}
		
		
	}
	
	
	public static void saveFile(CommonsMultipartFile[] files, String savePath) {

		
		String current = null;
		CommonsMultipartFile[] cpFile = files;
		int tot = cpFile.length;
//		org.springframework.web.method
//		FileWritingMessageHandler f = new FileWritingMessageHandler();
		//FileOutputStream out = null;
		if(savePath.equals("") || savePath == "" || savePath == null){
			try {
				
				savePath = new java.io.File( "." ).getCanonicalPath();
				current = savePath;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}else{
				current = savePath;
				current += "\\"+"uploadedFile";
				new File(current).mkdir();
				//current += "\\";
				
		}
		for(int i=0; i < tot; i++){
			
				byte dataToWrite[] = cpFile[i].getBytes();
				try {
					
					
					
					BufferedOutputStream stream =
							new BufferedOutputStream(
									new FileOutputStream(
											new File(
													current + File.separator + cpFile[i].getOriginalFilename())));
					
					stream.write(dataToWrite);
					//out.write(dataToWrite);
					stream.close();
					//System.out.println("Current dir:" + current);
					//System.out.println("You successfully uploaded " + cpFile[i].getOriginalFilename() + " into " + cpFile[i].getOriginalFilename() + "-uploaded !");
					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			
			
		}
		
		
	}
	public static void saveFile(List<FileXHTML> files){

		List<FileXHTML> cpFile = files;
		int tot = cpFile.size();
//		org.springframework.web.method
//		FileWritingMessageHandler f = new FileWritingMessageHandler();
		//FileOutputStream out = null;
		for(int i=0; i < tot; i++){
			FileXHTML tempX = cpFile.get(i);
			byte[] dataToWrite = tempX.getBytes();
			//System.out.println("BYTE DENTRO SAVE FILE tempX.getBytes();= " + tempX.getBytes().toString());
			//System.out.println("BYTE DENTRO SAVE FILE dataToWrite[;= " + dataToWrite.toString());
			try {
			String current = new java.io.File( "." ).getCanonicalPath();//CHIEDERE AD AMLETO
//			current += "\\"+"uploadedFile";
//			new File(current).mkdir();
			//current += "\\";
			
			current += "\\";
			current = current + cpFile.get(i).getOriginalFileName();
				//current = current + ".xhtml";

				BufferedOutputStream stream =
						new BufferedOutputStream(
								new FileOutputStream(
										new File(
												current)));
				
				stream.write(dataToWrite);
				
				//out.write(dataToWrite);
				
				
				
				stream.close();
				

				
				//stream.close();
				//System.out.println("Current dir:" + current);
				//System.out.println("You successfully uploaded " + cpFile.get(i).getOriginalFileName() + " into " + cpFile.get(i).getOriginalFileName() + "-uploaded !");
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	public static <T> T[] concatenate (T[] A, T[] B) {
	    int aLen = A.length;
	    int bLen = B.length;

	    @SuppressWarnings("unchecked")
	    T[] C = (T[]) Array.newInstance(A.getClass().getComponentType(), aLen+bLen);
	    System.arraycopy(A, 0, C, 0, aLen);
	    System.arraycopy(B, 0, C, aLen, bLen);

	    return C;
	}
}
